<template>
  <a-drawer
    :title="title"
    :visible="visible"
    :width="720"
    :wrapStyle="{height: '100%',overflow: 'auto',paddingBottom: '108px'}"
    @close="close"
  >
    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="add" type="primary" icon="plus">新增</a-button>
    </div>

    <!-- table区域 -->
    <div>

      <s-table
        ref="table"
        rowKey="id"
        :showPagination="false"
        :columns="columns"
        :data="loadData"
      >
        <span slot="action" slot-scope="text, record">
          <a @click="edit(record)">
            <a-icon type="edit"/>编辑
          </a>
          <a-divider type="vertical"/>
          <a-popconfirm title="确定删除吗?" @confirm="() => del(record.id)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </s-table>
    </div>

    <permission-data-rule-modal ref="modal" @ok="refreshTable"></permission-data-rule-modal>

  </a-drawer>
</template>

<script>
import { STable } from '@/components'
import { apiQueryPermRuleListByPermId, apiDelPermissionRule } from '@/api/system'
import PermissionDataRuleModal from './PermissionDataRuleModal'

const columns = [
  {
    title: '规则名称',
    dataIndex: 'ruleName',
    key: 'ruleName'
  },
  {
    title: '规则字段',
    dataIndex: 'ruleColumn',
    key: 'ruleColumn'
  },
  {
    title: '规则值',
    dataIndex: 'ruleValue',
    key: 'ruleValue'
  },
  {
    title: '操作',
    dataIndex: 'action',
    scopedSlots: { customRender: 'action' },
    align: 'center'
  }
]

export default {
  name: 'PermissionDataRuleList',
  components: {
    STable, PermissionDataRuleModal
  },
  data () {
    return {
      columns,
      title: '数据权限规则',
      visible: false,
      queryParam: {},
      loadData: param => {
        return apiQueryPermRuleListByPermId(Object.assign(param, this.queryParam)).then(res => {
          return res
        })
      },
      permissionId: ''
    }
  },
  methods: {
    show (title, record) {
      this.title = title
      this.visible = true
      if (record) {
        this.permissionId = record.permissionId
        this.queryParam.permissionId = record.permissionId
        this.refreshTable()
      }
    },
    close () {
      this.visible = false
    },
    add () {
      this.$refs.modal.show('新增', { permissionId: this.permissionId })
    },
    edit (record) {
      this.$refs.modal.show('编辑', record)
    },
    del (id) {
      apiDelPermissionRule({ id: id }).then(res => {
        this.$message.success(res.message)
      }).finally(() => {
        this.refreshTable()
      })
    },
    refreshTable () {
      this.$nextTick(() => {
        this.$refs.table.refresh(true)
      })
    }
  }
}
</script>

<style scoped>

</style>
